package com.example.hospital.controller;

import com.example.hospital.common.ApiResponse;
import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.entity.ExaminationOrder;
import com.example.hospital.mapper.ExaminationOrderMapper;
import com.example.hospital.service.OrderService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    private final ExaminationOrderMapper orderMapper;
    private final OrderService orderService;
    /**
     * 分页查询订单（按用户ID、医生ID、状态、订单类型筛选）
     * @param userId
     * @param doctorId
     * @param status
     * @param orderType
     * @param page
     * @param size
     * @return
     */
    public OrderController(ExaminationOrderMapper orderMapper, OrderService orderService) {
        this.orderMapper = orderMapper;
        this.orderService = orderService;
    }
    /**
     * 更新订单状态并自动生成待采集样本
     * @param orderId
     * @param status
     * @param remark
     * @return
     */
    @GetMapping
    public ApiResponse<PageResponse<ExaminationOrder>> list(@RequestParam(name = "userId", required = false) Long userId,
                                                            @RequestParam(name = "doctorId", required = false) Long doctorId,
                                                            @RequestParam(name = "status", required = false) Integer status,
                                                            @RequestParam(name = "orderType", required = false) String orderType,
                                                            @RequestParam(name = "page", defaultValue = "1") int page,
                                                            @RequestParam(name = "size", defaultValue = "10") int size) {
        int offset = (page - 1) * size;
        java.util.List<ExaminationOrder> list = orderMapper.list(userId, doctorId, status, orderType, size, offset);
        long total = orderMapper.count(userId, doctorId, status, orderType);
        return ApiResponse.success(PageResponse.of(total, page, size, list));
    }
    /**
     * 获取订单详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ApiResponse<ExaminationOrder> getById(@PathVariable("id") Long id) {
        return ApiResponse.success(orderMapper.findById(id));
    }
    /**
     * 更新订单状态并自动生成待采集样本
     * @param orderId
     * @param status
     * @param remark
     * @return
     */
    @PutMapping("/{orderId}/status")
    public ApiResponse<Boolean> updateStatus(@PathVariable("orderId") Long orderId,
                                             @RequestParam(name = "status") Integer status,
                                             @RequestParam(name = "remark", required = false) String remark) {
        return ApiResponse.success(orderService.updateStatusAndAutoSamples(orderId, status, remark));
    }
}